package com.ktgj.pay.utils;

import com.ktgj.common.core.utils.SpringUtils;
import com.ktgj.pay.domain.MoSendMail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Component
public class MailUtil {

	    // 邮件发送者
		@Autowired
		private static JavaMailSender javaMailSender;
		// 发送人
		private static String fromAddress;

		public static void send(SimpleMailMessage message){
	    	javaMailSender.send(message);
	    }

	    public static void send(MimeMessage message){
	    	javaMailSender.send(message);
	    }

		public JavaMailSender getJavaMailSender() {
			return javaMailSender;
		}

		public void setJavaMailSender(JavaMailSender javaMailSender) {
			MailUtil.javaMailSender = javaMailSender;
		}

		public String getFromAddress() {
			return fromAddress;
		}

		public void setFromAddress(String fromAddress) {
			MailUtil.fromAddress = fromAddress;
		}

		/**
		 * 	发送邮件 
		 * @param toAddress 邮件接收者，多者用逗号分隔
		 * @param subject 主题
		 * @param text 邮件正文(内容)
		 * @throws MessagingException
		 */
		public void sendMail(MoSendMail sendMail) throws MessagingException {
			// 创建邮件
			javaMailSender = SpringUtils.getBean(JavaMailSender.class);
			MimeMessage mime = javaMailSender.createMimeMessage();
			// 邮件包装工具   如果要添加附件，需要加true
			MimeMessageHelper helper = sendMail.isContainFJ()?new MimeMessageHelper(mime,true):new MimeMessageHelper(mime);
			// 主题
			helper.setSubject(sendMail.getTitle());
			// 发件人
			String fromAddresss = "invoice@ktgj.com";
			helper.setFrom(fromAddresss);
			// 收件人
			helper.setTo(sendMail.getToAddress());
			// 邮件正文
			helper.setText(sendMail.getContext());
			if(sendMail.isContainFJ()){
				File file = new File(sendMail.getFilePath());  //附件本地路径
				//邮件附件
				helper.addAttachment(file.getName(), file);  //添加附件	
			}
			// 发送邮件
			send(mime);
		}
		
		public static void sendLogMail(String context) throws MessagingException{
			MoSendMail sendMail = new MoSendMail();
			sendMail.setContainFJ(false);
			sendMail.setSend(true);
			sendMail.setTitle("日志报警");
			sendMail.setContext(context);
			sendMail.setToAddress("maming@jsj.com.cn");
			
			// 创建邮件
			MimeMessage mime = javaMailSender.createMimeMessage();
			// 邮件包装工具   如果要添加附件，需要加true
			MimeMessageHelper helper = sendMail.isContainFJ()?new MimeMessageHelper(mime,true):new MimeMessageHelper(mime);
			// 主题
			helper.setSubject(sendMail.getTitle());
			// 发件人
			helper.setFrom(fromAddress);
			// 收件人
			helper.setTo(sendMail.getToAddress());
			// 邮件正文
			helper.setText(sendMail.getContext());
			if(sendMail.isContainFJ()){
				File file = new File(sendMail.getFilePath());  //附件本地路径
				//邮件附件
				helper.addAttachment(file.getName(), file);  //添加附件	
			}
			// 发送邮件
			send(mime);
			
		}
		
		public static void sendMailMQ(MoSendMail sendMail) throws MessagingException {
			// 创建邮件
			MimeMessage mime = javaMailSender.createMimeMessage();
			// 邮件包装工具   如果要添加附件，需要加true
			MimeMessageHelper helper = sendMail.isContainFJ()?new MimeMessageHelper(mime,true):new MimeMessageHelper(mime);
			// 主题
			helper.setSubject(sendMail.getTitle());
			// 发件人
			helper.setFrom(fromAddress);
			// 收件人
			helper.setTo(sendMail.getToAddress());
			// 邮件正文
			helper.setText(sendMail.getContext());
			if(sendMail.isContainFJ()){
				File file = new File(sendMail.getFilePath());  //附件本地路径
				//邮件附件
				helper.addAttachment(file.getName(), file);  //添加附件	
			}
			// 发送邮件
			send(mime);
		}
		
		/**
		 * 预警邮件发送
		 * @param context
		 * @throws MessagingException
		 */
		public static void emailWarning(String context)throws MessagingException{
			MoSendMail sendMail = new MoSendMail();
			sendMail.setContainFJ(false);
			sendMail.setSend(true);
			sendMail.setTitle("发票服务通知");
			sendMail.setContext(context);//
			sendMail.setToAddress("maming@jsj.com.cn");
			
			// 创建邮件
			MimeMessage mime = javaMailSender.createMimeMessage();
			// 邮件包装工具   如果要添加附件，需要加true
			MimeMessageHelper helper = sendMail.isContainFJ()?new MimeMessageHelper(mime,true):new MimeMessageHelper(mime);
			// 主题
			helper.setSubject(sendMail.getTitle());
			// 发件人
			helper.setFrom(fromAddress);
			// 收件人
			helper.setTo(sendMail.getToAddress());
			// 邮件正文
			helper.setText(sendMail.getContext());
			if(sendMail.isContainFJ()){
				File file = new File(sendMail.getFilePath());  //附件本地路径
				//邮件附件
				helper.addAttachment(file.getName(), file);  //添加附件	
			}
			// 发送邮件
			send(mime);
		}
}
